package com.frame.pageform.remote;

import java.io.*;
import java.net.*;


public class RemoteCall {

	private String server;
	private String className;

	public RemoteCall(String server,String className){
		this.server=server;
		this.className=className;
	}
	public Object call(String method,Object[] parameters) throws Exception
	{
		String s="http://"+server+"/remote.call";
		Object obj=null;
		try{
			URL url = new URL(s);
			URLConnection connection=url.openConnection();
			connection.setUseCaches(true);
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestProperty("Content-type", "application/octest-stream");
			connection.setRequestProperty("Content-length", "" + -1);
			OutputStream outs = connection.getOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(outs);
			RemoteRequest rmr=new RemoteRequest(className,method);
			if(parameters!=null){
				for(int i=0;i<parameters.length;i++)
					rmr.addParameter(parameters[i]);
			}
			out.writeObject(rmr);
			out.flush();
			out.close();

			ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
			obj= in.readObject();
			in.close();
		}
		catch(Exception e){
			throw new Exception("远程调用发生错误",e);
		}
		if(obj instanceof Throwable)
			throw new Exception("远程调用服务端发生错误",(Throwable)obj);
		return obj;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getServer() {
		return server;
	}
	public void setServer(String server) {
		this.server = server;
	}

}
